/*
 * @(#)WordNetCollocations.java	1.0 08/01/07
 *
 * Copyright 2007 Fabio Gasparetti. All rights reserved.
 */

package org.tabularium.text.nlp;

import java.util.*;
import java.io.*;
import org.tabularium.text.nlp.wordnet.*;
import org.tabularium.text.nlp.wordnet.jwi.*;

/**
 * Extracts the noun collocations from the WordNet dictionary.
 * <p>
 * The extracted nouns have '-' and '_' characters converted to whitespaces.
 * <p>
 * It's better to use it just to build an in-memory textual dictionary because
 * it takes time to load the entire list.
 * 
 * @author Fabio Gasparetti
 * @version 1.0, 08/01/07
 */
public class WordNetCollocations extends Collocations {
	protected WordNet wordNet;

	public WordNetCollocations(WordNet wordNet) {
		super();
		this.wordNet = wordNet;
		init();
	}

	protected void init() {
		Iterator iter = wordNet.synsets(PartOfSpeech.NOUN);
		Set colls = new TreeSet();
		Synset s;
		String[] ww;
		int i;
		while (iter.hasNext()) {
			s = (Synset) iter.next();
			ww = wordNet.getWords(s);
			for (i = 0; i < ww.length; i++) {
				if ((ww[i].indexOf('_') >= 0) || (ww[i].indexOf('-') >= 0)
						|| (ww[i].indexOf(' ') >= 0)) {
					ww[i] = ww[i].replace('_', ' ');
					ww[i] = ww[i].replace('-', ' ');
					ww[i] = ww[i].toLowerCase();
					colls.add(ww[i]);
				}
			}
		}
		collocations = new String[colls.size()];
		iter = colls.iterator();
		i = 0;
		while (iter.hasNext())
			collocations[i++] = (String) iter.next();
	}

	// debug
	public static void main(String args[]) throws Exception {
		try {
			JwiWordNet wn = new JwiWordNet();
			wn.init();
			WordNetCollocations wnc = new WordNetCollocations(wn);
			FileWriter writer = new FileWriter("collocations.txt");
			for (int i = 0; i < wnc.collocations.length; i++) {
				System.out.println(wnc.collocations[i]);
				writer.write(wnc.collocations[i] + "\n");
			}

		} catch (Exception ex) {
			System.out.println(ex.toString());
			ex.printStackTrace();
		}
	}

}
